import 'package:enjoy_plus/utils/http.dart';
import 'package:enjoy_plus/utils/toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';

class NoticeDetail extends StatefulWidget {
  const NoticeDetail({super.key});

  @override
  State<NoticeDetail> createState() => _NoticeDetailState();
}

class _NoticeDetailState extends State<NoticeDetail> {
  Map notifyDetail = {
    "id": "3",
    // 带 html 的文本，可以称之为 富文本
    "content":
        "<p>尊敬的住户:</p>\r\n<p>你们好!现本小区已进入装修阶段,进出苑区人员日益增多,管理处为加强小区人员进出管理,确保苑区的安全和谐,须对苑区住户办理业主卡,住户须凭业主卡进出苑区。请业主于近期内到管理处办理,谢谢合作!</p>\r\n<p>办理业主卡需带资料:</p>\r\n<p>一、业主及家人:</p>\r\n<p>1寸照片2张,身份证复印件1张</p>\r\n<p>二、租住户:</p>\r\n<p>租房合同复件;身份证复件1份;1寸照片2张</p>\r\n<p>三、公司员工:</p>\r\n<p>租房合同复件(限租住户);公司营业执照复件1份;公司介绍信;身份证复件1张;1寸照片2张。</p>",
    "title": "小区关于办理业主卡的通知",
    "createdAt": "2022-09-11 14:16:57",
    "creatorName": "传智教育",
  };

  // 在生命周期中解析 路由跳转携带而来的参数
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // 获取路由跳转携带的参数
    Map arguments = ModalRoute.of(context)?.settings.arguments as Map;
    // 打印参数
    // print(arguments);
    // // 赋值给 notifyDetail
    // notifyDetail = arguments;

    getDetail(arguments['id']);
  }

  getDetail(String id) async {
    final res = await http.get('/announcement/$id');
    // print(res);
    if (res['code'] == 10000) {
      ToastUtil.showSuccess('详情获取成功');
      setState(() {
        notifyDetail = res['data'];
      });
    }else{
      ToastUtil.showError('数据获取失败');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('公告详情')),
      body: ListView(
        children: [
          Container(
            padding: const EdgeInsets.all(10),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // 标题
                Text(
                  notifyDetail['title'] ?? '',
                  style: const TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(height: 10),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      notifyDetail['creatorName'] ?? '',
                      style: const TextStyle(color: Colors.grey),
                    ),
                    Text(
                      notifyDetail['createdAt'] ?? '',
                      style: const TextStyle(color: Colors.grey),
                    ),
                  ],
                ),
                const SizedBox(height: 10),
                // 内容
                Html(data: notifyDetail['content'] ?? ''),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
